import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:snaplist/snaplist_view.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/common/ui/AppTheme.dart';
import 'package:xm_app/common/ui/ui_image.dart';
import 'package:xm_app/widget/PhotoViewSimpleScreen.dart';
import 'GankController.dart';

class GankPage extends GetView<GankController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.withOpacity(.05),
      appBar: AppBar(
        title: Text(
          'Gank Page',
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: AppTheme.pinkColor,
        actions: [
          IconButton(
              icon: Icon(
                Icons.recommend,
                color: Colors.white,
              ),
              onPressed: () => controller.getHotData()),
          IconButton(
              icon: Icon(
                Icons.assignment_ind,
                color: Colors.white,
              ),
              onPressed: () => controller.getCategoryData())
        ],
      ),
      body: Container(
        child: GetBuilder<GankController>(
            init: GankController(),
            builder: (_) {
              final Size cardSize = Size(300.0, 460.0);
              return AnimatedSwitcher(
                duration: Duration(milliseconds: 800),
                child: SnapList(
                  key: ValueKey(controller.type),
                  snaplistController: controller.snaplistController,
                  padding: EdgeInsets.only(left: (MediaQuery.of(context).size.width - cardSize.width) / 2),
                  sizeProvider: (index, data) => cardSize,
                  separatorProvider: (index, data) => Size(10.0, 10.0),
                  positionUpdate: (int index) {
                    if (controller.type == "normal") {
                      if (index == controller.categoryData.data.length - 2) {
                        controller.loadMore();
                      }
                    }
                  },
                  builder: (context, index, data) {
                    final item = controller.categoryData.data[index];
                    return GestureDetector(
                      onTap: () {
                        Get.dialog(PhotoViewSimpleScreen(
                          imageProvider: CachedNetworkImageProvider(item.url),
                          heroTag: 'simple',
                        ));
                      },
                      child: Card(
                        elevation: 5.0,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(16.0)),
                        ),
                        ////对Widget截取的行为，比如这里 Clip.antiAlias 指抗锯齿
                        clipBehavior: Clip.antiAlias,
                        child: buildNetImage(item.url),
                      ),
                    );
                  },
                  count: controller.categoryData?.data?.length ?? 0,
                ),
              );
            }),
      ),
    );
  }
}
